//
//  Device8RelayCell.m
//  LazyHome
//
//  Created by yh on 2018/5/7.
//  Copyright © 2018年 yh. All rights reserved.
//

#import "Device8RelayCell.h"
#import "LHDeviceInfo.h"
#import "LHDeviceManger.h"
#import "LHManager.h"
#import "TCPManager.h"
#import "NSMutableData+BufferHandler.h"
#import "LHCmd.h"
#import <MJExtension/MJExtension.h>

@implementation Device8RelayCell


char getbit(char sour , char index){
    char compare = (0b00000001 << index);
    return  (sour & compare) == compare;
}

char setBit(char sour , char index ,char yn){
    if (yn) {
        char compare = (0b00000001 << index);
        return  (sour | compare);
    }else{
        char compare = 0b11111110;
        compare = ~compare;
        compare = compare << index;
        compare = ~compare;
        return  (sour & compare);
    }
}

- (void)awakeFromNib {
    [super awakeFromNib];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceInfoChange:) name:NOTI_TCP_MESSAGE_CAME object:nil];
    // Initialization code
    self.status = 0xff;
}


- (void)deviceInfoChange:(NSNotification *)noti{
    LHMessage *message = noti.object;
     NSDictionary *dict = [message.data mj_JSONObject];
    if ([message.cmdData toUShort] == CMD_S2P_SENDEVICE_BASEINFO) {
        NSString *deviceId = dict[@"id"];
        NSString *name = dict[@"name"];
        NSString *type = dict[@"type"];
        if ([deviceId isEqualToString:self.deviceId]) {
            self.devTitleLabel.text = [NSString stringWithFormat:@"%@:%@",name,deviceId];
            self.onlineStatusLabel.text = [self.model .isonline boolValue] ? @"在线" : @"离线";
        }
    }else if (CMD_S2P_SENDEVICE_STATUSINFO == [message.cmdData toUShort]){
        NSString *deviceId = dict[@"deviceId"];
        if ([deviceId isEqualToString:self.deviceId]) {
            NSNumber *number = dict[@"status"];
            [self setButtons:[number intValue] view:self.contentView];
        }
    }
    
    
//    NSDictionary *dict = noti.object;
//    NSString *devId = dict[@"devId"];
//    if ([devId isEqualToString:self.model.devId]) {
//        //更新
//        NSString * status = dict[@"devStatus"];
//        [self setButtons:[status intValue] view:self.contentView];
//    }
}

-(void)setDeviceId:(NSString *)deviceId{
    _deviceId = deviceId;
    self.devTitleLabel.text = _deviceId;
    [self getDeviceStatusWithDeviceId:deviceId];
    [self getDeviceBaseInfoWithDeviceId:deviceId];
}


- (void)getDeviceBaseInfoWithDeviceId:(NSString *)deviceId{
    [[LHManager shareManager] getDeviceBaseInfoWithDeviceId:deviceId];
}

- (void)getDeviceStatusWithDeviceId:(NSString *)deviceId{
    [[LHManager shareManager] getDeviceStateInfoWithDeviceId:deviceId];
}

- (void)setDataModel:(id)model handle:(id<DeviceCellHandle>)handle{
   // [self setButtons:status view:self.contentView];
    self.delegate = handle;
    self.model = model;
    self.devTitleLabel.text = self.model .name;
    self.onlineStatusLabel.text = [self.model .isonline boolValue] ? @"在线" : @"离线";
}


- (void)setHandle:(id<DeviceCellHandle>)handle{
    self.delegate = handle;
}

- (void)setButtons:(char)status view:(UIView *)viewContent{
    _status = status;
    for (UIView *view in viewContent.subviews) {
        
        if (view.subviews.count == 0) {
            if ([view isKindOfClass:[UIButton class]]) {
                NSInteger index = view.tag;
                ((UIButton *)view).selected = !getbit(status, 7 - index);
            }
        }else{
            if ([view isKindOfClass:[UIButton class]]) {
                NSInteger index = view.tag;
                ((UIButton *)view).selected = !getbit(status, 7 - index);
            }else{
                [self setButtons:status view:view];
            }
            
        }
    }
    
  
}

- (void)device8RelayCell:(id)device8RelayCell changeDevice8RelayStatus:(unsigned char)status button:(UIButton *)button index:(int)index{
    
}


//检验状态
- (BOOL)check:(short)data dest:(short)dest{
    return (data & dest) == ~dest;
}

- (IBAction)buttonAction:(id)sender {
    UIButton *button = sender;
//    button.selected = !button.selected;
    NSInteger buttonIndex = button.tag;
    unsigned char changeStatus = setBit(self.status, 7 - buttonIndex, button.selected);
//    self.status = changeStatus;
    NSLog(@"%x",changeStatus);
    
    if (self.delegate) {
        [self.delegate device8RelayCell:self changeDevice8RelayStatus:changeStatus button:button index:(int)buttonIndex];
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
